<template>
<div class="row header">
  <div class="col-xs-12">
    <div class="user pull-right">
      <div class="item dropdown">
        <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
          <img v-attr="src:header.userImgSrc">
        </a>
        <ul class="dropdown-menu dropdown-menu-right">
          <li class="dropdown-header">Admin</li>
          <li class="divider"></li>
          <li class="link">
            <a href="javascript:;" v-on='click: onReboot'>重启</a>
          </li>
          <li class="divider"></li>
          <li class="link">
            <a href="javascript:;" v-on='click: runShell("serverReset.sh")'>重置主服务器</a>
          </li>
          <li class="link" v-if="productCfg.product=='Media'">
            <a href="javascript:;" v-on='click: runShell("clearMediaFile.sh")'>清空媒体文件</a>
          </li>
          <li class="link">
            <a href="javascript:;" v-on='click: runShell("factoryReset.sh")'>恢复出厂设置</a>
          </li>
        </ul>
      </div>
      <div class="item dropdown" v-if="false">
       <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
          <i class="fa fa-bell-o"></i>
        </a>
        <ul class="dropdown-menu dropdown-menu-right">
          <li class="dropdown-header">Notifications</li>
          <li class="divider"></li>
          <li>
            <a href="javascript:;">TODO</a>
          </li>
        </ul>
      </div>
    </div>
    <div class="meta" v-if="false">
      <div class="page">
        Dashboard
      </div>
      <div class="breadcrumb-links">
        Home / Dashboard
      </div>
    </div>    
  </div>
</div>
</template>

<script>
var log = require('../lib/log');
var api = require('../lib/api');
var settings = require('../lib/settings');
var productCfg = settings.productCfg;

module.exports = {
  replace: true,
  props: ['header', 'productCfg'],
  methods: {
    doRestart: function() {
      var paramlist = {
        shName: 'restart.sh'
      }; 

      api.utils.sendCommonCmd('runShell', paramlist);
      setTimeout(function() {
        swal.close();
        //router.go('/');
        location.reload(true);
      }, productCfg.bootTime);      
    },
    onReboot: function(e) {
      e && e.preventDefault();
      swal({
        title: "Are you sure?",
        text: "You need to wait for minute.",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, restart it!",
        closeOnConfirm: false,
        showLoaderOnConfirm: true,      
      }, 
      function() {
        api.auth.login(productCfg.dftModule, function(res) {
          if (res.code === 0) {
            this.doRestart();
          } else {
            log.showErrorWithRes(res);           
          }          
        }.bind(this));
      }.bind(this));
    },
    runShell: function(shName) {
      swal({
        title: "Are you sure?",
        text: "The Operation Can NOT Be Restored!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, reset it!",
        closeOnConfirm: false,
        showLoaderOnConfirm: true,      
      }, 
      function() {
        api.auth.login(productCfg.dftModule, function(res) {
          var paramlist = {
            shName: shName
          };
          api.utils.sendCommonCmd('runShell', paramlist, function(res) {
            if ((res === undefined)
              || (res.code != 0)
              || (res.data.result.indexOf('success') === -1)) {
              log.DEBUG_TRACE(res.data.result);
              log.showError(res.msg ? res.msg: 'Code:' + res.code);
              return;
            }

            this.doRestart();
          }.bind(this));         
        }.bind(this));
      }.bind(this));
    }
  }
}
</script>

<style>
</style>